/**
 * @description 用户画像》 品牌联系强度》 用户价值
 *@author yhw
 */
import {
  interactiveQuery,
  buyQuery,
  allQuery
} from '../services/UserValue';
import {
  parse
} from 'qs';
import error from '../utils/tips';

export default {

  namespace: 'UserValue',

  state: {
    interactiveData: null,
    interactiveLoading: false,
    buyData: null,
    buyLoading: false,
    allData: null,
    allLoading: false
  },

  subscriptions: {},

  effects: { * interactiveQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'interactiveLoading'
      });

      const {
        data
      } = yield call(interactiveQuery, parse(payload));

      if (data) {

        let temp = {
            category: [],
            value: []
          },
          colors = ['#ceb180', '#fff2cc', '#fbe5d6', '#f8cbad', '#ededed', '#deebf7', '#bdd7ee', '#adb9ca', '#f2f2f2', '#d9d9d9', '#ffc86b', '#ffb22f'];
        data.data && data.data.map(function(item, index) {
          temp.category.push(item.name);
          temp.value.push({
            name: item.name,
            value: item.value,
            itemStyle: {
              normal: {
                color: colors[index]
              }
            }
          });
        });

        yield put({
          type: 'interactiveQuerySuccess',
          payload: {
            interactiveData: temp,
          },
        });
      } else {
        error("获取用户互动价值失败");
      }
    },
    //===================================
    * buyQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'buyLoading'
      });

      const {
        data
      } = yield call(buyQuery, parse(payload));

      if (data) {

        let temp = {
            category: [],
            value: []
          },
          colors = ['#ceb180', '#fff2cc', '#fbe5d6', '#f8cbad', '#ededed', '#deebf7', '#bdd7ee', '#adb9ca', '#f2f2f2', '#d9d9d9', '#ffc86b', '#ffb22f'];
        data.data && data.data.map(function(item, index) {
          temp.category.push(item.name);
          temp.value.push({
            name: item.name,
            value: item.value,
            itemStyle: {
              normal: {
                color: colors[index]
              }
            }
          });
        });

        yield put({
          type: 'buyQuerySuccess',
          payload: {
            buyData: temp,
          },
        });
      } else {
        error("获取用户购买价值失败");
      }
    },
    //===================================
    * allQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'allLoading'
      });

      const {
        data
      } = yield call(allQuery, parse(payload));

      if (data) {

        let temp = {
            category: [],
            value: []
          },
          colors = ['#ceb180', '#fff2cc', '#fbe5d6', '#f8cbad', '#ededed', '#deebf7', '#bdd7ee', '#adb9ca', '#f2f2f2', '#d9d9d9', '#ffc86b', '#ffb22f'];
        data.data && data.data.map(function(item, index) {
          temp.category.push(item.name);
          temp.value.push({
            name: item.name,
            value: item.value,
            itemStyle: {
              normal: {
                color: colors[index]
              }
            }
          });
        });

        yield put({
          type: 'allQuerySuccess',
          payload: {
            allData: temp,
          },
        });
      } else {
        error("获取用户综合价值失败");
      }
    },

  },

  reducers: {
    interactiveLoading(state) {
      return {...state,
        interactiveLoading: true
      };
    },
    interactiveQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        interactiveLoading: false
      };
    },
    //===================================
    buyLoading(state) {
      return {...state,
        buyLoading: true
      };
    },
    buyQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        buyLoading: false
      };
    },
    //===================================
    allLoading(state) {
      return {...state,
        allLoading: true
      };
    },
    allQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        allLoading: false
      };
    },

  },
};